﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using hedefgrup.reports.renault.database;

namespace hedefgrup.reports.common.SSRD
{
    public class Report_SSRD_AuthorizedServices : Report_SSRD_Abstract
    {
        protected override void SetCriteriaList()
        {
            Criterias.Add(new Criteria_SSRD_SSMPeriod() { Context = Context });
            Criterias.Add(new Criteria_Roll() { Context = Context });
            Criterias.Add(new Criteria_EndMonth() { Context = Context });
            Criterias.Add(new Criteria_Organization() { Context = Context });
        }

        internal class StorageService
        {
            internal string ServiceName;
            internal int Count;
            internal int S3_1_2;
            internal int S6_1_2;
            internal int S10_1_2;

            internal double GetScore()
            {
                return (double)(S3_1_2 + S6_1_2 + S10_1_2) / ((double)Count * 3d);
            }
        }

        internal class Storage
        {
            internal SortedList<string, StorageService> ResultList = new SortedList<string, StorageService>();
            internal StorageService Total = new StorageService() { ServiceName = "Genel" };

            internal void Add(string serviceName, int count, int s3, int s6, int s10)
            {
                StorageService storageService;

                if (ResultList.ContainsKey(serviceName))
                {
                    storageService = ResultList[serviceName];
                }
                else
                {
                    storageService = new StorageService();
                    storageService.ServiceName = serviceName;
                    ResultList.Add(serviceName, storageService);
                }

                storageService.Count += count;
                Total.Count += count;

                if (s3 == 1 || s3 == 2)
                {
                    storageService.S3_1_2 += count;
                    Total.S3_1_2 += count;
                }

                if (s6 == 1 || s6 == 2)
                {
                    storageService.S6_1_2 += count;
                    Total.S6_1_2 += count;
                }

                if (s10 == 1 || s10 == 2)
                {
                    storageService.S10_1_2 += count;
                    Total.S10_1_2 += count;
                }
            }
        }

        public override ReportResult GenerateReport()
        {
            DataTable reportData = db.query
            (
                "SELECT SERVIS, CNT = COUNT(SERVIS), S3, S6, S10 FROM " + Context.TableName + " " +
                GetSqlStatementFilter() +
                " GROUP BY SERVIS, S3, S6, S10 ",
                base.GetSqlStatementParameters()
            );

            Storage reportStorage = new Storage();

            foreach (DataRow reportRow in reportData.Rows)
            {
                string serviceName = reportRow["SERVIS"].ToString();
                if (!string.IsNullOrEmpty(serviceName))
                {
                    reportStorage.Add
                    (
                        GetAuthorizedServiceName(serviceName),
                        Convert.ToInt32(reportRow["CNT"]),
                        reportRow["S3"] == DBNull.Value ? 0 : Convert.ToInt32(reportRow["S3"]),
                        reportRow["S6"] == DBNull.Value ? 0 : Convert.ToInt32(reportRow["S6"]),
                        reportRow["S10"] == DBNull.Value ? 0 : Convert.ToInt32(reportRow["S10"])
                    );
                }
            }

            ReportResult results = new ReportResult(this);

            Row headerRow = new Row();
            results.HeaderRows.Add(headerRow);

            headerRow.Cells.Add(new Cell() { Text = "Yetkili Servis", Bold = true });
            headerRow.Cells.Add(new Cell() { Text = "Baz", Bold = true });
            headerRow.Cells.Add(new Cell() { Text = "Genel Skor", Bold = true });

            Row totalRow = new Row();
            results.Rows.Add(totalRow);
            totalRow.Cells.Add(new Cell() { Text = reportStorage.Total.ServiceName });
            totalRow.Cells.Add(new Cell() { Text = reportStorage.Total.Count.ToString() });
            totalRow.Cells.Add(new Cell() { Text = (100 * reportStorage.Total.GetScore()).ToString("0.0") });

            foreach (StorageService storageService in reportStorage.ResultList.Values)
            {
                Row serviceRow = new Row();
                results.Rows.Add(serviceRow);
                serviceRow.Cells.Add(new Cell() { Text = GetAuthorizedServiceName(storageService.ServiceName) });
                serviceRow.Cells.Add(new Cell() { Text = storageService.Count.ToString() });
                serviceRow.Cells.Add(new Cell() { Text = (100 * storageService.GetScore()).ToString("0.0") });
            }

            return results;
        }

        public override bool SupportsProject(string projectName)
        {
            return projectName == Global.PROJECT_SSRD;
        }

        public override string SortPrefix
        {
            get { return "SSRD_00"; }
        }

        public override string DisplayName
        {
            get { return "Yetkili Servis Raporu"; }
        }
    }
}
